home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1867 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.1 KB  |  45 lines

  1. Path: news.infi.net!usenet
  2. From: nngis@norfolk.infi.net (Greg DiGiorgio)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Checking for a file => Does it exist (Help)
  5. Date: 16 Jan 1996 02:27:31 GMT
  6. Organization: Customer of InfiNet
  7. Message-ID: <4df2ej$pqr@news.infi.net>
  8. References: <4d9k6fINNnja@faatcrl.faa.gov>
  9. NNTP-Posting-Host: h-hanuman.norfolk.infi.net
  10. Mime-Version: 1.0
  11. X-Newsreader: WinVN 0.99.3
  12.  
  13. In article <4d9k6fINNnja@faatcrl.faa.gov>, afrawert@faatcrl.faa.gov 
  14. says...
  15. >
  16. >
  17. >
  18. >                        I'm having a problem just checking to see if a 
  19. file
  20. >exists. Does anyone have a simple piece of code that I can use?
  21. >
  22. >                                afrawert@faatcrl.faa.gov
  23. >
  24. >
  25. A simple way is to try and open the file. If it opens successfully, then 
  26. it exists. If not, then either it does not exist or you don't have rad 
  27. permission (UNIX).
  28.  
  29. #include <stdio.h>
  30. #define NOTEXIST    0
  31. #define EXIST       1
  32. int file_exist(char filespec[]) {
  33.     FILE *f;
  34.     f=fopen(filespec,"rt");
  35.     if (!f) return(NOTEXIST);
  36.     fclose(f);
  37.     return(EXIST);
  38. }
  39.  
  40. Of course, there is some overhead associated with open/close a file.
  41.  
  42. Hope this helps,
  43. Greg DiGiorgio
  44.  
  45.